﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BitPortal.Models;
using BitPortal.Utils;
namespace BitPortal.Business
{
	public static class WorkWarrantyBLL
	{
        /* ********************************* INSERT - DELETE - UPDATE ****************************** */


		public static int Insert(this BitPortal.Models.WorkWarranty workWarranty)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();


            ErrorReport error = extBitPortal.ErrorReports.SingleOrDefault(e => e.ErrorID == workWarranty.ErrorID);
            error.StateError = (int)StateError.Daduyet;
          
			extBitPortal.WorkWarranties.AddObject(workWarranty);
			return extBitPortal.SaveChanges();
		}

        public static int DeletePermanently(Int64 workID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			WorkWarranty work = extBitPortal.WorkWarranties.SingleOrDefault(pp => pp.WorkWarrantyID == workID);
			extBitPortal.WorkWarranties.DeleteObject(work);
			return extBitPortal.SaveChanges();
		}


		public static int  Update(WorkWarranty workWarranty)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			WorkWarranty work = extBitPortal.WorkWarranties.SingleOrDefault(pp => pp.WorkWarrantyID == workWarranty.WorkWarrantyID);
			work = workWarranty;
			return extBitPortal.SaveChanges();
		}


        /* *************************************** GET WORKS ******************************** */

        /// <summary>
        /// Get an work by workID
        /// </summary>
        /// <param name="workID"></param>
        /// <returns></returns>
		public static WorkWarranty GetByWorkID(Int64 workID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			return extBitPortal.WorkWarranties.SingleOrDefault(pp => pp.WorkWarrantyID == workID);
		}

        /// <summary>
        /// Get work of an user who is working the work
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
		public static IQueryable<WorkWarranty> GetByUserID(Int64 userID)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			IQueryable<WorkWarranty> list =from pp in extBitPortal.WorkWarranties
										   where pp.UserId.ToInt64TryParse(0)== userID
										   select pp;
			
			return list;
		}

        /// <summary>
        /// Get all users who working this work
        /// </summary>
        /// <param name="workId"></param>
        /// <returns></returns>
		public static string GetUersWorking(Int64 workId)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			return extBitPortal.WorkWarranties.SingleOrDefault(pp => pp.WorkWarrantyID == workId).UsersRepair;
		}

        /// <summary>
        /// Get works by priority
        /// </summary>
        /// <param name="priority"></param>
        /// <returns></returns>
		public static IQueryable<WorkWarranty> GetByPriority(int priority)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			return extBitPortal.WorkWarranties.Where(pp => pp.Priority == priority);
		}

        /// <summary>
        /// Get works by state work
        /// </summary>
        /// <param name="stateWork"></param>
        /// <returns></returns>
		public static IQueryable<WorkWarranty> GetByStateWork(int stateWork)
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			return extBitPortal.WorkWarranties.Where(pp => pp.StateWork == stateWork);
		}

        /// <summary>
        /// Get all works includes work has IsDeleted is true
        /// </summary>
        /// <returns></returns>
    
		public static IQueryable<WorkWarranty> GetAll()
		{
			ExtendedBitPortalEntities extBitPortal = new ExtendedBitPortalEntities();

			IQueryable<WorkWarranty> list =  from p in extBitPortal.WorkWarranties
                                             orderby p.WorkWarrantyID descending, p.StateWork descending
								             select p;
			return list;
		}

       
    }
}